package com.study.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.study.bean.Student;
import com.study.mapper.StudentMapper;
import com.study.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    @Override
    public Page selectByPage(Integer currentPage, Integer pageSize) {
        InputStream is = null;
        SqlSession sqlSession = null;
        Page page = null;
        try {
            is = Resources.getResourceAsStream("MybatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
             sqlSession = sqlSessionFactory.openSession(true);
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            page  = PageHelper.startPage(currentPage, pageSize);

             mapper.selectStudentAll();


        } catch (IOException e) {

             e.printStackTrace();

        }finally {
            if (sqlSession != null){
                sqlSession.close();
            }


            if (is !=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }


        return page;


    }

    @Override
    public void addStu(Student stu) {
        InputStream is = null;
        SqlSession sqlSession = null;
        Page page = null;
        try {
            is = Resources.getResourceAsStream("MybatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession(true);
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            mapper.addStu(stu);

        } catch (IOException e) {

            e.printStackTrace();

        }finally {
            if (sqlSession != null){
                sqlSession.close();
            }


            if (is !=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }

    }

    /*
        修改数据
     */
    @Override
    public void updateStu(Student student) {

        InputStream is = null;
        SqlSession sqlSession = null;

        try{
             is = Resources.getResourceAsStream("MybatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            sqlSession = sqlSessionFactory.openSession(true);

            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            mapper.updateStu(student);


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if (sqlSession != null){
                sqlSession.close();
            }


            if (is !=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }



    }

    @Override
    public void deleteStu(String number) {

        InputStream is = null;
        SqlSession sqlSession = null;

        try{
            is = Resources.getResourceAsStream("MybatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            sqlSession = sqlSessionFactory.openSession(true);

            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            mapper.deleteStu(number);

        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if (sqlSession != null){
                sqlSession.close();
            }


            if (is !=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
